package com.xoa.service.party;

import com.xoa.dao.party.FourFormMapper;
import com.xoa.model.party.FourForm;
import com.xoa.model.party.FourFormWithBLOBs;
import com.xoa.util.DateFormat;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by gsb on 2017/11/25.
 */
@Service
public class FourFormService {

    @Resource
    private FourFormMapper fourFormMapper;
    /*
    添加
     */
    public ToJson<FourForm> insert(String beginTime, String tbr, String lxdh, String tbsj , FourFormWithBLOBs fourForm){
        ToJson<FourForm> json = new ToJson<FourForm>();
        try {
            if(beginTime != null && beginTime !=""){
                Date date= DateFormat.getDate(beginTime);
                fourForm.setOpenTime(date);
            }
            fourForm.setFillingMan(tbr);
            fourForm.setContactNumber(lxdh);
            fourForm.setReportDate(tbsj);
            fourFormMapper.insert(fourForm);
            json.setMsg("ok");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("ok");
            json.setFlag(0);
        }
        return json;

    }
    /*
修改
 */
    public ToJson<FourForm> update(String beginTime, String tbr, String lxdh, String tbsj , FourFormWithBLOBs fourForm){
        ToJson<FourForm> json = new ToJson<FourForm>();
        try {
            if(beginTime != null && beginTime !=""){
                Date date= DateFormat.getDate(beginTime);
                fourForm.setOpenTime(date);
            }
            fourForm.setFillingMan(tbr);
            fourForm.setContactNumber(lxdh);
            fourForm.setReportDate(tbsj);
            fourFormMapper.updateByPrimaryKeySelective(fourForm);
            json.setMsg("ok");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("ok");
            json.setFlag(0);
        }
        return json;

    }
    /*
    删除
     */
    public ToJson<FourForm> deleteByPrimaryKey(Integer sid){
        ToJson<FourForm> json = new ToJson<FourForm>();
        try {
            fourFormMapper.deleteByPrimaryKey(sid);
            json.setMsg("ok");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }
    /*
    详情
     */
    public ToJson<FourForm> selectByPrimaryKey(Integer sid){
        ToJson<FourForm> json = new ToJson<FourForm>();
        try {
            FourForm fourForm = fourFormMapper.selectByPrimaryKey(sid);
            json.setObject(fourForm);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }
    /*
    列表
     */
    public ToJson<FourFormWithBLOBs> query(Map<String,Object> maps, Integer page,
                                           Integer pageSize, boolean useFlag){
        ToJson<FourFormWithBLOBs> json = new ToJson<FourFormWithBLOBs>();
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        maps.put("page", pageParams);
        try {
            List<FourFormWithBLOBs> fourFormList = fourFormMapper.query(maps);
            json.setObj(fourFormList);
            json.setTotleNum(pageParams.getTotal());
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();

        }
        return json;

    }
}
